import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: () => import('@/views/LoginView.vue')
    },
    {
        name: 'confirm',
        path: '/confirm', // 修正：相对路径
        component: () => import('@/views/ConfirmView.vue'),
        meta: { requiresAuth: true, disableBack: true }
    },
    {
        name: 'exam',
        path: '/exam/:paperId', // 修正：参数占位符
        component: () => import('@/views/ExamView.vue'),
        meta: { requiresAuth: true, disableBack: true },
        props: true // 推荐：启用props接收参数
    }

]

const router = createRouter({
    history: createWebHistory(),
    routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
    console.log(to, 666);
    if (to.path === '/login') {
        localStorage.clear()
        next()  // 登录页直接放行
        return
    }
    if (localStorage.getItem('exam_token')) {
        next()
        return '/login'
    } else {
        next('/login')
    }

})
export default router